package com.example.module_home.paging


import androidx.paging.PagingSource
import com.example.module_home.Article
import com.example.module_home.network.Repository


class ArticleDataSource : PagingSource<Int, Article>() {
    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Article> {
        val initKey = params.key ?: 0
        val articleData = Repository.fetchArticle(initKey)
        val articleList = mutableListOf<Article>()
        articleList.addAll(articleData.data.datas ?: emptyList())

        val prevKey = if (initKey == 0) null else initKey - 1
        return LoadResult.Page(
            articleList,
            prevKey = prevKey,
            nextKey = initKey.plus(1)
        )


    }
}